<template>
	<view class="swiper-mana" style="position: relative;">
		<!-- 第四部分开始 -->
		<view class="box1" style="margin-top: 30rpx;">
			<u-upload @on-remove="removeSwip" :showProgress="false" @on-uploaded="uploadSuccessSwi"
				:action="`https://job.ydylmold.cn/base/upload/file`" :fileList="acFileSwi"
				:formData="{fileCode:144,fileType:100,fileParaName:'file'}" width="100" height="100"
				:maxCount="allNum">
			</u-upload>
		</view>
	</view>
</template>


<script>
	//导入裁剪组件
	import {
		queryPicList,
		queryUsrMainPicNum,
		deleteUsrMainPic,
		insertUsrMainPic,
		queryUsrCompanyStationList,
		insertCompanyStation,
		deleteUsrCompanyStationById
	} from '@/common/api/auth.js'
	import {
		getToken,
		setToken
	} from '@/utils/token.js'
	export default {
	
		data() {
			return {
				swipList:[],
				sendSwip:[],
				acFileSwi:[],
				allNum:1
			}
		},

		onLoad: function(option) {
			this.init()
		},
		
		methods: {
			//查找轮播图
			queryPicList() {
				if(getToken('useNum'))
				return queryPicList({
					craftsmanId: getToken('useNum'),
					moduleNo: 'ydylAppUsr001',
					state: 1,
					owner:getToken('useNum')
				})
			},
			// 轮播图梳数量
			queryUsrMainPicNum() {
				return queryUsrMainPicNum({
					craftsmanId: getToken('useNum'),
					moduleNo: 'ydylAppUsr001'
				})
			},
			// 初始化数据
			init() {
				//获取企业轮播图、轮播图数量、企业认证状态、企业更多资料
				Promise.all([this.queryPicList(), this.queryUsrMainPicNum()
				]).then(([
					res1, res2= []
				]) => {
					if (res1.statusCode == 200) {
						this.swipList = res1.data
						res1.data && res1.data.forEach(m => {
							m.showImg =
								'https://www.ydylmold.cn/images/ydylapp/usrMainPic/usrCraftsmanImage/' + m
								.picPath
							this.acFileSwi.push({
								url: 'https://www.ydylmold.cn/images/ydylapp/usrMainPic/usrCraftsmanImage/' +
									m.picPath
							})
						})
					}
					if (res2.data && res2.data.code == 1) {
						this.allNum =res2.data && res2.data.map && res2.data.map.picNum
					}
				
				})
			},
			
			uploadSuccessSwi(e) {
				this.sendSwip = []
				e.forEach(v => {
					if (v.response && v.response.code == 200) {
						this.sendSwip.push(v.response.data)
					}
				})
				this.saveSwip()
			},
			saveSwip() { //保存上传的轮播图
				this.sendSwip.forEach(async m => {
					let result = await this.loadImg(m)
					if (result.data && result.data.code == 1) {
						let acItem = result.data && result.data.data
						this.swipList.push(acItem)
					} else {
						this.$u.toast('上传失败')
					}
				})
			},
			loadImg(path) {
				return insertUsrMainPic({
					state: '1',
					moduleNo: 'ydylAppUsr001',
					craftsmanId: getToken('useNum'),
					isGeneralize: 1,
					imagePic: path
				})
			},
			removeSwip(e, list, index, delItem) { //删除上传的轮播图
				this.acFileSwi = list
				let acPath = ''
				if (delItem.response && delItem.response.code == 200) {
					acPath = delItem.response.data
				} else {
					acPath = delItem.url
				}
				let idx2 = this.swipList.findIndex(v => {
					return acPath.includes(v.picPath) || acPath == v.picPath
				})
				if (idx2 != -1) {
					deleteUsrMainPic({
						picId: this.swipList[idx2].picId
					}).then(res => {
						this.swipList.splice(e, 1)
					})
				}
			},
		}
	}
</script>


<style scoped>
	
	.swiper-mana{
		height: 100vh;
		background-color: #FFFFFF;
		padding: 0rpx 40rpx;
		box-sizing: border-box;
	}
	.img-view {
		background-color: #ffffff;
		border-bottom: 1rpx solid #f0f0f0;
		display: flex;
		flex-wrap: wrap;
		align-items: center;
	}
.img-list{
	width: 100%;
}
	.add-pic {
		width: 196rpx;
		height: 196rpx;
		border: 1rpx solid #a8a8a8;
		border-radius: 10rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		margin: 10rpx;
	}

	.add-pic-txt {
		margin-top: 30rpx;
	}

	.btn-box {
		position: fixed;
		bottom: 0;
		width: 100%;
		background-color: #ffffff;
		padding: 40rpx 0 30rpx 0;
	}

	.icondel {
		position: absolute;
		top: -20rpx;
		right: -20rpx;
		font-size: 40rpx;
	}
</style>
